package com.apes.framework.plugin.esb.parser.element;

import com.apes.framework.plugin.esb.processor.ChainProcessor;
import com.apes.framework.plugin.esb.processor.SampleChainProcessor;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 功能：chain元素解析器
 *
 * @author xul
 * @create 2018-04-16 14:25
 */
@Component
public class ChainElementParser extends AbstractNestedProcessorParser {

    private static Map<String, ChainProcessor> processors = new ConcurrentHashMap();

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"chain"};

    /**
     * 功能：可解析的元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    @Override
    public ChainProcessor parse(ApesElement element) {
        String key = getKey(element);
        ChainProcessor processor = processors.get(key);
        if (processor == null) {
            processor = super.parse(element);
            processors.put(key, processor);
        }
        return processor;
    }

    @Override
    <T extends Class> T getChainProcessorClass() {
        return (T) SampleChainProcessor.class;
    }


    private String getKey(ApesElement element) {
        return methodWithVersion(element.getAttribute("method").toString(), element.getAttribute("version").toString());
    }

    private static String methodWithVersion(String method, String version) {
        return method + "#" + version;
    }

}
